<?php

namespace app\ports\controller\publics;

use think\Db;

/**
 * 数据表操作
 */
class Tables
{

	protected static $instance;

	public $config = [
		"prefix" => "fd_",
	];

	/**
	 * 获取实例对象
	 * @return Tables|object
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {
			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 创建分红交易存储表（日存储）
	 *
	 * @param [type] $tables
	 * @param [type] $proname
	 * @return void
	 */
	public function bonus_trans_days_store_tables($tables, $times)
	{
		$table = "";
		try {
			$createTab = $tables;
			$res = $this->checkCreate($createTab);
			if (!$res) {
				$str = "`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
						`comid` int(11) DEFAULT NULL COMMENT '所属机构|公司ID',
						`agid`  int(11) DEFAULT NULL COMMENT '代理商ID',
						`proid` int(11) DEFAULT NULL COMMENT '产品ID',
						`nums` int(11) DEFAULT NULL COMMENT '笔数',
						`trans` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '交易类型',
						`cards` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '卡类型',
						`route` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '收款方式',
						`transAmt` decimal(16,2) unsigned DEFAULT '0.00' COMMENT '交易金额',
						`times` int(10) unsigned DEFAULT '0' COMMENT '交易时间 yyyyMMdd',
						`processType` tinyint(1) unsigned DEFAULT '1' COMMENT '处理状态 【1 - 未处理 ，2 - 已处理】',
						`createtime` int(10) unsigned DEFAULT '0' COMMENT '创建时间',
						PRIMARY KEY (`id`) USING BTREE";
				$table = $this->createTable($createTab, $str, 'InnoDB', $times . "分红按天交易存储表");
			} else {
				$table = $createTab;
			}
		} finally {
			return $table;
		}
	}

	/**
	 * 创建分红交易存储表（月存储）
	 *
	 * @param [type] $tables
	 * @param [type] $proname
	 * @return void
	 */
	public function bonus_trans_store_tables($tables, $times)
	{
		$table = "";
		try {
			$createTab = $tables;
			$res = $this->checkCreate($createTab);
			if (!$res) {
				$str = "`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
						`comid` int(11) DEFAULT NULL COMMENT '所属机构|公司ID',
						`agid`  int(11) DEFAULT NULL COMMENT '代理商ID',
						`proid` int(11) DEFAULT NULL COMMENT '产品ID',
						`nums` int(11) DEFAULT NULL COMMENT '笔数',
						`trans` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '交易类型',
						`cards` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '卡类型',
						`route` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '收款方式',
						`transAmt` decimal(16,2) unsigned DEFAULT '0.00' COMMENT '交易金额',
						`times` int(10) unsigned DEFAULT '0' COMMENT '交易时间 yyyyMM',
						`processType` tinyint(1) unsigned DEFAULT '1' COMMENT '处理状态 【1 - 未处理 ，2 - 已处理】',
						`createtime` int(10) unsigned DEFAULT '0' COMMENT '创建时间',
						PRIMARY KEY (`id`) USING BTREE";
				$table = $this->createTable($createTab, $str, 'InnoDB', $times . "分红交易存储表");
			} else {
				$table = $createTab;
			}
		} finally {
			return $table;
		}
	}

	/**
	 * 创建数据表
	 *
	 * @param string $createTable   创建表名
	 * @param string $sqlStr		创建字段
	 * @param string $comment		表字段
	 * @return string
	 */
	public function createTable($createTable, $sqlStr, $engine = "InnoDB",  $comment = "数据表")
	{
		$table = "";
		try {
			if (!empty($createTable) && !empty($sqlStr)) {
				# 创建验证
				$isExist =  $this->checkCreate($createTable);
				if (!$isExist) {
					$sql_str = "CREATE TABLE `" . $this->config["prefix"] . $createTable . "` (" . $sqlStr . ") ENGINE=" . $engine . " DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='" . $comment . "';";
					Db::execute($sql_str);
				}
				# 是否创建成功
				$isExist = $this->checkCreate($createTable);
				if ($isExist) {
					$table = $createTable;
				}
			}
		} finally {
			return $table;
		}
	}

	/**
	 * 查看表是否存在
	 *
	 * @param [type] $table
	 * @return boolean
	 */
	public function checkCreate($table)
	{
		return Db::query("SHOW TABLES like '" . $this->config["prefix"] . $table . "'");
	}
}
